package com.bkgy.controller.lzDaNew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.lzDaNew.entity.LzDaNew;
import com.bkgy.controller.lzDaNew.entity.pdo.WorkStatusBarReq;
import com.bkgy.controller.lzDaNew.entity.pdo.WorkStatusBarRes;
import com.bkgy.controller.lzDaNew.mapper.ILzDaNewMapper;
import com.bkgy.controller.lzDaNew.service.ILzDaNewService;
import com.bkgy.sys.common.entity.ResultData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class LzDaNewServiceImpl extends ServiceImpl<ILzDaNewMapper, LzDaNew> implements ILzDaNewService {
    private final ILzDaNewMapper mapper;

    @Override
    public ResultData<WorkStatusBarRes> workStateBar(WorkStatusBarReq req) {
        List<WorkStatusBarRes> resList = new ArrayList<>();
        if ("chaoqi".equals(req.getDateType())) {
            List<LzDaNew> list = mapper.selectChaoqi();
            for (LzDaNew obj : list) {
                WorkStatusBarRes res = new WorkStatusBarRes();
                res.setRollNo(obj.getRollNo());
                res.setTarget(obj.getStatus() + "");
                resList.add(res);
            }
        } else {
            LambdaQueryWrapper<LzDaNew> w = new LambdaQueryWrapper<>();
            w.orderByDesc(LzDaNew::getFirstTime).last(" FETCH FIRST 5 ROWS ONLY");
            List<LzDaNew> list = list(w);
            for (LzDaNew obj : list) {
                WorkStatusBarRes res = new WorkStatusBarRes();
                res.setRollNo(obj.getRollNo());
                if ("gongli".equals(req.getDateType())) {
                    res.setTarget(obj.getWarningRollingKilometers() == null ? "0" : obj.getWarningRollingKilometers() + "");
                    res.setActual("0");
                } else if ("cucao".equals(req.getDateType())) {
                    res.setTarget(StringUtils.isNotEmpty(obj.getRoughOsNow()) ? obj.getRoughOsNow() : "0");
                    res.setActual(StringUtils.isNotEmpty(obj.getOriginalRoughCoefficient()) ? obj.getOriginalRoughCoefficient() : "0");
                }
                resList.add(res);
            }
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }
}
